<script type="text/javascript">
    $(document).ready(function() {

        $('#petition').dataTable();

    });

    function verifyPetition(pet_id) {
//        var task = "verify";
//        $.post('process_status_salary.php', {task: task, pet_id: pet_id}, function(data) {
//            window.location.href = "index.php?pages=verify_approved_salary";
//        });

    
        var task = "verify";
//        $.post('pet_status_process.php', {task: task, id: id}, function(data) {
//            window.location.href = "index.php?pages=pet_verify_approved";
//        });
        
        $('<div>').dialog2({
            autoOpen: true,
            title: "ตรวจสอบ",
            content: "dlg_verify_salary.php?pet_id=" + pet_id + "&task=" + task,
            id: "modal-date"
        });

    }

    function cancelPetition(pet_id) {
        var task = "cancel";
        //alert(id);

//        $('<div>').dialog2({
//            autoOpen: true,
//            title: "ยกเลิกฎีกา",
//            content: "dlg_cancel_salary.php?pet_id=" + pet_id,
//            id: "modal-cancel"
//        });

        if (confirm("คุณแน่ใจใช่ไหมที่ต้องการ ยกเลิก สัญญานี้")) {

            $('<div>').dialog2({
                autoOpen: true,
                title: "ยกเลิกฎีกา",
                content: "dlg_cancel.php?tblname=tbl_salary&id=" + pet_id,
                id: "modal-cancel"
            });
        }

    }

//    function approvedPetition(pet_id) {
//        var task = "approved";
//
//        $('<div>').dialog2({
//            autoOpen: true,
//            title: "อนุมัติ",
//            content: "dlg_date_approved.php?pet_id=" + pet_id + "&task=" + task,
//            id: "modal-date"
//        });
//
//
//    }
//    function disapprovalPetition(pet_id) {
//        var task = "disapproval";
//
//        $.post('process_status_salary.php', {task: task, pet_id: pet_id}, function(data) {
//            window.location.href = "index.php?pages=verify_approved_salary";
//        });
//
//    }

</script>

<?php
$sql = "SELECT  tbl_salary.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name, sum(salary) as salary, verify_status, approved_status,status "
        . "FROM tbl_salary, expenses_lvl1, expenses_lvl2, expenses_lvl3, panngan2, panngan3 "
        . "WHERE lvl1_id = '" . $_SESSION["lvl1_id"] . "' 
            and expenses_lvl1.expenses1_id = ex1_id 
            and expenses_lvl2.expenses2_id = ex2_id 
            and expenses_lvl3.expenses3_id = ex3_id 
            and panngan2.panngan2_id = tbl_salary.panngan2_id 
            and panngan3.panngan3_id = tbl_salary.panngan3_id
	GROUP BY tbl_salary.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name, verify_status, approved_status,status
	";
$db = new myDBS;
$db->selectDB("account");
$result = $db->runSQL($sql);
?>

<table id="petition" class="table table-bordered table-hover" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>#</th>
            <th>เลขที่ฎีกา</th>
<!--            <th>แผนงาน</th>
            <th>งาน</th>-->
            <th>ประเภท</th>
            <th>รายการ</th>
            <th>จำนวนเงิน</th>
            <th>ตรวจรับฎีกา</th>
            <!--<th>อนุมัติ</th>-->
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 0;
        while ($rows = $db->fetch_object($result)) {
            $i++;
//                                <td>$rows->panngan2_name</td>
//                    <td>$rows->panngan3_name</td>

            echo ($rows->status == "ยกเลิก")? "<tr class='error'>":"<tr>";
            echo "

                    <td>$i</td>
                    <td>$rows->pet_id</td>
                    <td>$rows->expenses2_name</td>
                    <td>$rows->expenses3_name</td>
                    <td>" . number_format($rows->salary, 2) . "</td>";
            echo "<td>";

            if ($rows->verify_status == "รอตรวจสอบ" && $rows->status == "") {

                echo "<a href='javascript:verifyPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ผ่าน'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:cancelPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ไม่ผ่าน'><i class='icon-remove'></i></a>";
            } elseif ($rows->status == "ยกเลิก") {
                echo $rows->status;
            } else {
                echo $rows->verify_status;
            }
//            if ($rows->verify_status == "รอตรวจสอบ") {
//
//                echo "<a href='javascript:verifyPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ผ่าน'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:cancelPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ไม่ผ่าน'><i class='icon-remove'></i></a>";
//            } else {
//                echo $rows->verify_status;
//            }
            echo "</td>";
//            echo "<td><p class='text-center'>";
//            if ($rows->verify_status == "ผ่าน") {
//                if ($rows->approved_status == "รออนุมัติ") {
//                    echo "<a href='javascript:approvedPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-remove'></i></a>";
//                } else {
//                    if ($rows->approved_status == "อนุมัติ") {
//                        echo "<font color='blue'>";
//                    }
//                    echo $rows->approved_status;
//                }
//            } else {
//                echo "รอการตรวจสอบ";
//            }
//            echo "</p></td>";
            echo "</tr>";
        }
        ?>
    </tbody>
</table>